import pygame
from pygame.locals import *
from plane import *
import time


def main():
    # 1.创建一个窗口，用来显示内容
    screen = pygame.display.set_mode((480, 800), 0, 32)

    # 2.创建一个和窗口大小的图片，用来充当背景
    image_file_path = './feiji/background.png'
    background = pygame.image.load(image_file_path).convert()

    # 3. 创建一个飞机对象
    hero_plane = HeroPlane(screen, "hero")

    # 4. 创建一个敌人飞机
    enemy_plane = EnemyPlane(screen, "enemy")

    clock = pygame.time.Clock()
    # 4.把背景图片放到窗口中显示
    while True:
        screen.blit(background, (0, 0))
        hero_plane.display()
        enemy_plane.display()
        enemy_plane.move()
        enemy_plane.launch_bullet()
        # 判断是否是点击了退出按钮
        for event in pygame.event.get():
            if event.type == QUIT:
                print("exit")
                exit()

        keys = pygame.key.get_pressed()
        if keys[K_a] or keys[K_LEFT]:
            print('left')
            hero_plane.move_left()
        elif keys[K_d] or keys[K_RIGHT]:
            print('right')
            hero_plane.move_right()
        elif keys[K_w] or keys[K_UP]:
            print('upper')
            hero_plane.move_up()
        elif keys[K_s] or keys[K_DOWN]:
            print('down')
            hero_plane.move_down()
        elif keys[K_SPACE]:
            print('space')
            hero_plane.launch_bullet()

        time.sleep(0.01)
        clock.tick(120)
        pygame.display.update()


if __name__ == '__main__':
    main()
